﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace SpriteExtractor.View
{
    public class FileSystemService : ViewModel.IFileSystemService
    {

        OpenFileDialog _openFileDlg;
        SaveFileDialog _saveFileDlg;

        public string BrowseFileToOpen(KeyValuePair<string, string>[] extensions)
        {
            if (_openFileDlg == null)
            {
                _openFileDlg = new OpenFileDialog();
            }

            _openFileDlg.CheckPathExists = true;
            _openFileDlg.Filter = ToFilter(extensions);
            

            if (_openFileDlg.ShowDialog() == true)
                return _openFileDlg.FileName;
            return null;
        }

        public string BrowseFileToSave(KeyValuePair<string, string>[] extensions)
        {
            if (_saveFileDlg == null)
            {
                _saveFileDlg = new SaveFileDialog();
            }

            _saveFileDlg.AddExtension = true;
            _saveFileDlg.Filter = ToFilter(extensions);
            

            if (_saveFileDlg.ShowDialog() == true)
                return _saveFileDlg.FileName;
            return null;
        }

        private string ToFilter(KeyValuePair<string, string>[] extensions)
        {
            string filter = "";
            foreach (var ext in extensions)
                filter += String.Format("{0}|{1}|", ext.Key, ext.Value);
            return filter.Substring(0, filter.Length - 1);
        }
    }
}
